package com.gdth.bank.model;

import java.util.Date;
import java.util.List;

import rx.Observable;
import rx.Subscriber;

import com.gdth.bank.BankApplication;
import com.gdth.database.bean.TAppGncd;
import com.gdth.database.bean.TAppGncdDao;
import com.gdth.database.bean.TAppYh;
import com.gdth.database.bean.TAppYhDao;

import org.greenrobot.greendao.query.DeleteQuery;
import org.greenrobot.greendao.query.QueryBuilder;


public class TAppYhModel {
	
	/**
	 * 保存登陆信息
	 * @param user
	 */
	public Observable<TAppYh> saveUserInformation(final TAppYh user){
		return Observable.create(new Observable.OnSubscribe<TAppYh>() {

			@Override
			public void call(final Subscriber<? super TAppYh> arg0) {
				TAppYhDao userDao = BankApplication.getDaoSession().getTAppYhDao();
				QueryBuilder<TAppYh> userqb = userDao.queryBuilder();
				userqb.where(TAppYhDao.Properties.Zh.eq(user.getZh()));
				
				List<TAppYh> userList = userqb.list();
				if (userList != null && userList.size() >0){
					user.setId(userList.get(0).getId());
				}
				
		    	user.setZhdlsj(new Date());
		    	
		    	userDao.insertOrReplace(user);
		    	
		    	TAppGncdDao menuDao = BankApplication.getDaoSession().getTAppGncdDao();
		    	QueryBuilder<TAppGncd> qb = menuDao.queryBuilder();
				qb.where(TAppGncdDao.Properties.Userid.eq(user.getId()));
				DeleteQuery<TAppGncd> deleteKhxx = qb.buildDelete();
				deleteKhxx.executeDeleteWithoutDetachingEntities();
				if (user.getMenuList() != null && user.getMenuList().size() > 0) {
					for (TAppGncd menu : user.getMenuList()){
						menu.setUserid(user.getId().toString());
					}
			    	menuDao.insertInTx(user.getMenuList());
				}
		    	arg0.onNext(user);
		    	arg0.onCompleted();
			}
		});
	}
	
	/**
	 * 离线登陆
	 * @param user
	 * @return
	 */
	public Observable<TAppYh> getUserByLocal(final TAppYh user){
		return Observable.create(new Observable.OnSubscribe<TAppYh>() {

			@Override
			public void call(final Subscriber<? super TAppYh> arg0) {
				TAppYhDao dao = BankApplication.getDaoSession().getTAppYhDao();
				QueryBuilder<TAppYh> qb = dao.queryBuilder();
				qb.or(TAppYhDao.Properties.Zh.eq(user.getZh()), 
						TAppYhDao.Properties.Zongheid.eq(user.getZh()), TAppYhDao.Properties.Xindaiid.eq(user.getZh()));
				
				List<TAppYh> uList = qb.list();
				if (uList == null || uList.size() == 0){
					Throwable mThrowable = new Throwable("账号错误");
					arg0.onError(mThrowable);
					
					return;
				}
				
				TAppYh currentYh = uList.get(0);
				
				if (currentYh.getDlcs() != null && currentYh.getDlcs() > 5) {
					Throwable mThrowable = new Throwable("该账号已锁定。请联系管理员");
					arg0.onError(mThrowable);
					return;
				}
				
				qb.where(TAppYhDao.Properties.Mm.eq(user.getMm()), qb.or(TAppYhDao.Properties.Zh.eq(user.getZh()), 
						TAppYhDao.Properties.Zongheid.eq(user.getZh()), TAppYhDao.Properties.Xindaiid.eq(user.getZh())));
				uList = qb.list();
				if (uList != null && uList.size() > 0){
					TAppYh mUser = uList.get(0);
					mUser.setZhdlsj(new Date());
					mUser.setDlcs(0);
			    	dao.insertOrReplace(mUser);
			    	
			    	TAppGncdDao menuDao = BankApplication.getDaoSession().getTAppGncdDao();
			    	QueryBuilder<TAppGncd> qbMenu = menuDao.queryBuilder();
			    	qbMenu.where(TAppGncdDao.Properties.Userid.eq(mUser.getId()));
			    	List<TAppGncd> menus = qbMenu.list();
//					mUser.getMenuList().addAll(menus);
			    	mUser.setMenuList(menus);
					arg0.onNext(mUser);
					arg0.onCompleted();
				} else {
					int dlcs = 1;
					if (currentYh.getDlcs() != null) {
						dlcs = currentYh.getDlcs() + 1;
					}
					
					if (dlcs > 5) {
						Throwable mThrowable = new Throwable("该账号已锁定。请联系管理员");
						arg0.onError(mThrowable);
						return;
					}
					
					int sycs = 5 - dlcs;
					
					currentYh.setDlcs(dlcs);
					dao.insertOrReplace(currentYh);
					
					Throwable mThrowable = new Throwable("输错密码"+dlcs+"次，还有"+sycs+"次机会");
					arg0.onError(mThrowable);
				}
			}
		});
	}
}
